package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户管理
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    @RequestMapping("getUsername")
    public Result getUsername(){
        try{
            SecurityContext context = SecurityContextHolder.getContext();
            Authentication authentication = context.getAuthentication();
            UserDetails userDetails= (UserDetails) authentication.getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,userDetails.getUsername());
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
    //新增
    @RequestMapping("/add")
    public Result add(Integer[] roleIds, @RequestBody User user){
        try{
            userService.add(roleIds,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.findPage(queryPageBean);
        return pageResult;
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        User user = userService.findById(id);
        if(user != null){
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS);
            result.setData(user);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_USER_FAIL);
    }

    //根据用户id查询对应的所有角色id
    @RequestMapping("/findRoleByUserId")
    public Result findRoleIdsByUserId(Integer id){
        try{
            List<Integer> userIds =
                    userService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,userIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }



    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Role> userList = userService.findAll();
        if(userList != null && userList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(userList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    //查询所有
    @RequestMapping("/findRoleAll")
    public Result findRoleAll(){
        List<Role> roleList = userService.findRoleAll();
        if(roleList != null && roleList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(roleList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }


    //编辑
    @RequestMapping("/edit")
    public Result edit(Integer[] roleIds, @RequestBody User user){
        try{
            userService.edit(roleIds,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    //删除用户
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try{
            userService.deleteById(id);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }



}
